﻿using System;

namespace Conv
{
    public class SubsamplFeatureMap:Layer
    {
        public SubsamplFeatureMap(Layer FM, int resampleRatio, Func<double, double> activationFunc, Func<double, double> activationFuncDerivative)
        {
            Width = FM.Width / resampleRatio;
            Height = FM.Height / resampleRatio;
            //
            Msk = new Mask(1,1);
            Neurons = new Neuron[Width, Height];
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    Neurons[i, j] = new Neuron(){ActivationFunc = activationFunc,ActivationFuncDerivative = activationFuncDerivative};
                    ConnectNeuronToFMap(FM, Neurons[i, j], resampleRatio,Msk.Weights[0,0],Msk.Biass, i, j);
                }
            }
        }
        private void ConnectNeuronToFMap(Layer fm, Neuron neuron, int resampleRatio,Weight weight, Weight biass, int left, int up)
        {
            for (int i = 0; i < resampleRatio; i++)
            {
                for (int j = 0; j < resampleRatio; j++)
                {
                    neuron.Inputs.Add(new Connection() { From = fm.Neurons[left + i, up + j], To = neuron, Weight = weight });
                }
            }

            //add biass
            neuron.Inputs.Add(new Connection() { From = BiasNeuron, To = neuron, Weight = biass });
        } 

       
    }
}